<?php

/**
 * BuddyPress - Members Loop
 *
 * Querystring is set via AJAX in _inc/ajax.php - bp_dtheme_object_filter() - NOT ANYMORE! I FIXED THAT, QUERY NOW SET WITH WP API, SEE BELOW
 *
 * @package BuddyPress
 * @subpackage bp-default
 */

?>

<?php //do_action( 'bp_before_members_loop' ); ?>

<?php
// QUERY MEMBERS WITH WP API, GET MEMBER IDs AND INCLUDE IN THE BUDDYPRESS MEMBERS LOOP
		
	// SOME HELPER FUNCTIONS
		$dirURL = site_url('short-sale-assistants');
		$dirQURL = $dirURL . '?filter=true';
		function selectedOption() { echo ' selected="selected"'; }
		
	// RATING QUERY
		$QueryRating = array(); // DEFINE SO WE DON'T GET AN ERROR
		if ( isset($_GET['rating']) ) {
			$ratingVal = $_GET['rating'];
			$ratingQStr = '&rating=' . $ratingVal;
			$QueryRating = array(
				'key' => 'avgRating',
				'value' => $ratingVal,
				'compare' => '>=',
			);
		} else {
			$ratingVal = ''; // DEFINE SO WE DON'T GET AN ERROR
		}
	// ACCEPTING FILES QUERY
		$QueryAcceptingFiles = array(); // DEFINE SO WE DON'T GET AN ERROR
		if ( isset($_GET['acceptingFiles']) ) {
			$acceptingFilesVal = $_GET['acceptingFiles'];
			$acceptingFilesQStr = '&acceptingFiles=' . $acceptingFilesVal;
			$QueryAcceptingFiles = array(
				'key' => 'AcceptingNewFiles',
				'value' => $acceptingFilesVal,
				'compare' => 'LIKE'
			);
		} else {
			$acceptingFilesVal = ''; // DEFINE SO WE DON'T GET AN ERROR
		}
		
	// SET MEMBER QUERY ARGUMENTS
		$args = array(
			'role' => 'assistant',
			//'meta_key' => 'ratingWieght',
			//'meta_value_num' => '',
			//'meta_compare' => '',
			'meta_query' => array( $QueryRating, $QueryAcceptingFiles ),
			'orderby' => 'display_name',
			'order' => 'ASC',
			//'search' => '',
		 );
		 
	// RUN THE MEMBER QUERY AND COLLECT MEMBER IDs
		$members = get_users( $args );
		$memberIds = '0';
		foreach( $members as $member ) {
			$memberIds = $memberIds . ', ' . $member->ID;
		}
		
?>
	
<div id="memberFilters">
    <span>Show Assistants by </span>
        <!-- RATING -->
            <select onchange="window.location.href = '<?php echo $dirQURL . $acceptingFilesQStr . '\'+$(this).val()+\'' . "'"; ?>;">
                <option value="">rating</option>
                <option value="&rating=5"<?php if ( $ratingVal == '5' ) { selectedOption(); } ?>>5 stars</option>
                <option value="&rating=4"<?php if ( $ratingVal == '4' ) { selectedOption(); } ?>>4 stars +</option>
                <option value="&rating=3"<?php if ( $ratingVal == '3' ) { selectedOption(); } ?>>3 stars +</option>
                <option value="&rating=2"<?php if ( $ratingVal == '2' ) { selectedOption(); } ?>>2 stars +</option>
                <option value="&rating=1"<?php if ( $ratingVal == '1' ) { selectedOption(); } ?>>1 star +</option>
            </select>
        <!-- ACCEPTING FILES -->
            <select onchange="window.location.href = '<?php echo $dirQURL . $ratingQStr . '\'+$(this).val()+\'' . "'"; ?>;">
                <option value="">accepting files</option>
                <option value="&acceptingFiles=Yes"<?php if ( $acceptingFilesVal == 'Yes' ) { selectedOption(); } ?>>Yes</option>
                <option value="&acceptingFiles=No"<?php if ( $acceptingFilesVal == 'No' ) { selectedOption(); } ?>>Not Accepting</option>
            </select>
</div>

<div style="clear:both;"></div>

<?php
// THE BUDDYPRESS MEMBERS LOOP - QUERY SET ABOVE
	if ( bp_has_members( array( 'include' => $memberIds ) ) ) : ?>
        
		<div id="pag-top" class="pagination">
			<div class="pag-count" id="member-dir-count-top">
				<?php bp_members_pagination_count(); ?>
			</div>
			<div class="pagination-links" id="member-dir-pag-top">
				<?php bp_members_pagination_links(); ?>
			</div>
		</div>
	
		<?php do_action( 'bp_before_directory_members_list' ); ?>
	
		<ul id="members-list" class="item-list" role="main">
	
		<?php while ( bp_members() ) : bp_the_member(); ?>
	
			<li>
				<div class="item-title">
					<a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a>
				</div>
				<div class="item-avatar">
					<a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar( 'type=full' ); ?></a>
				</div>
	
				<div class="item">
					<?php do_action( 'bp_directory_members_item' ); ?>
					<p class="membersDirectoryRating"><?php ssa_rating_show( bp_get_member_user_id() ); ?></p>
					<p><strong>Accepting New Files:</strong> <?php bp_member_profile_data( 'field=Accepting New Files:' ); ?></p>
					<p><strong>About:</strong> <?php bp_member_profile_data( 'field=About:' ); ?></p>
					<!--<p><strong>Short Sales Processed:</strong> <?php //bp_member_profile_data( 'field=Short Sales Processed?' ); ?></p>-->
				</div>
	
				<div style="clear: both;"></div>
			</li>
	
		<?php endwhile; ?>
	
		</ul>
	
		<?php do_action( 'bp_after_directory_members_list' ); ?>
	
		<?php bp_member_hidden_fields(); ?>
	
	
		<div id="pag-bottom" class="pagination">
			<div class="pag-count" id="member-dir-count-bottom">
				<?php bp_members_pagination_count(); ?>
			</div>
			<div class="pagination-links" id="member-dir-pag-bottom">
				<?php bp_members_pagination_links(); ?>
			</div>
		</div>
	
	<?php else: ?>
	
		<div id="message" class="info">
			<p><?php _e( "Sorry, no Assistants were found.", 'buddypress' ); ?></p>
		</div>
	
	<?php endif; ?>

<?php do_action( 'bp_after_members_loop' ); ?>
